<script lang="ts" setup>
import GoButton from '@/components/basic/GoButton.vue'
import { computed, reactive, ref } from 'vue'
import DropdownMenuItem from '@/components/basic/DropdownMenuItem.vue'
import DefaultImg from '@/assets/map/bikeLocus/Speed.png'
import HoverImg from '@/assets/map/bikeLocus/hoverSpeed.png'
const emits = defineEmits(['stop', 'continue', 'speed-change'])
const handleClick = (type: 'stop' | 'continue' | 'speed-change') => {
  emits(type)
}
const hover = ref<boolean>(false)
const commandList = computed(() => dropdownMenu.map((item) => item.id))
const isActive = computed(() => commandList.value.some((comman) => comman.startsWith('un')))
const currentImg = computed(() => {
  return hover.value || isActive.value ? HoverImg : DefaultImg
})

const dropdownMenu = reactive([
  {
    id: '8x',
    name: '8x'
  },
  {
    id: '6x',
    name: '6x'
  },
  {
    id: '4x',
    name: '4x'
  },
  {
    id: '1x',
    name: '1x'
  },
  {
    id: '0.25x',
    name: '0.25x'
  },
  {
    id: '0.5x',
    name: '0.5x'
  }
])

const handleCommand = (command: string) => {
  const isActive = command.startsWith('un')
  if (isActive) return
  const index = dropdownMenu.findIndex((item) => {
    if (isActive) {
      return item.id === command.slice(2)
    } else {
      return item.id === command
    }
  })
  if (index < 0) return
  dropdownMenu.forEach((item) => (item.id.startsWith('un') ? (item.id = item.id.slice(2)) : item))
  dropdownMenu[index].id = 'un' + command
  emits('speed-change', +command.split('x')[0])
}
</script>

<template>
  <div class="locus-control-group">
    <el-dropdown popper-class="locus-control-group-dropdown" placement="top" trigger="click" @command="handleCommand" leading-36>
      <div
        flex
        items-center
        @mouseover="hover = true"
        @mouseout="hover = false"
        p-x-5
        box-border
        :class="isActive ? 'tools-title__active' : ''"
        class="hover:color-primary color-normal-text cursor-pointer"
      >
        <img w-20 h-20 :src="currentImg" />
        <span ml-10>倍速</span>
        <el-icon class="el-icon--right"><ArrowDown /></el-icon>
      </div>
      <template #dropdown>
        <el-dropdown-menu ref="SpeedMenu">
          <dropdown-menu-item
            v-for="(item, index) in dropdownMenu"
            :key="index"
            :id="item.id"
            :name="item.name"
            :image="false"
          />
        </el-dropdown-menu>
      </template>
    </el-dropdown>
    <el-divider direction="vertical" />
    <go-button icon-name="stop1" @click="handleClick('stop')">
      <template #text>
        <span>暂停</span>
      </template>
    </go-button>
    <el-divider direction="vertical" />
    <go-button icon-name="continue1" @click="handleClick('continue')">
      <template #text>
        <span>继续</span>
      </template>
    </go-button>
  </div>
</template>

<style scoped lang="scss">
.locus-control-group {
  position: absolute;
  bottom: 20px;
  right: 70px;
  z-index: 200;
  background-color: #fff;
  padding: 5px 10px;
  box-shadow: 0px 0px 10px 1px rgba(0, 0, 0, 0.1);
  border-radius: 5px 5px 5px 5px;
  .map-button {
    background: #ffffff;
    border-radius: 5px 5px 5px 5px;
    line-height: 36px;
    padding: 0 5px;
    > span {
      vertical-align: baseline;
      margin-left: 10px;
    }
  }
  .map-button + .map-button {
    margin-left: 10px;
  }
}
.tools-title__active {
  color: var(--xy-primary-color);
}
</style>
